
let app = getApp();
const util = require('../../util/util.js')
Page({

  //页面初始数据
  data: {
    name: '',
    department: '',
    identity: '',
    phone: '',
    userId: '',
    openId: '',
    visitor: false,
    student: false,
    loaded: false,
    serviceURL: '',
  },

  //页面显示
  onShow() {
    let res1 = dd.getStorageSync({ key: 'currentUser' });
    console.log("res:" + res1.data.userId + "---authcode:" + app.globalData.authCode);
    dd.getAuthCode({
      success: (res) => {
        console.log("---authcode:" + res.authCode);
        this.setData({
          userId: res1.data.userId, //钉钉Id
          name: app.globalData.userName,
          department: app.globalData.department,
          identity: app.globalData.identity,
          phone: app.globalData.phone,
          identityId: app.globalData.identityId,
          departmentId: app.globalData.departmentId,
        })
        dd.httpRequest({
          url: app.userUrl + '/getDingDingUserInfo',
          method: 'POST',
          data: {
            authCode: res.authCode
          },
          dataType: 'json',
          success: (res) => {
            console.log(res)
            this.setData({
              openId: res.result.openId, //openID
            })
          },
          fail: (res) => {
            // dd.alert({
            //   content: '网络异常',
            // })
          }
        })
      },
    })
  },


  logout() {
    console.log("----------------")
    dd.alert({
      content: '是否确认注销当前账号？点击确定后将返回到登录页面',
      buttonText: '确定',
      success() {
        dd.httpRequest({
          headers: {
            "Content-Type": "application/json"
          },
          url: app.userUrl + '/DingDing/AccountManagement/deleteDingDingIdByDingDingId',
          method: 'POST',
          data: JSON.stringify({
            dingdingId: app.globalData.dingdingId
          }),
          dataType: 'text',
          success: (res) => {
            if (res.data == "fail") {
              dd.showToast({
                content: '切换账号失败，请重试',
                duration: 2000
              })
              return
            }
            dd.showToast({
              content: '注销成功',
              duration: 2000
            })
            dd.removeStorageSync({
              key: 'currentUser',
            })
            if(app.globalData.departmentType == "教育局" || app.globalData.departmentType == "区教育局") {
              app.globalData.isSupervising = false
              app.globalData.superviseSchoolId = 0
              app.globalData.superviseSchoolName = ""
              app.globalData.superviseId = 0
              app.globalData.isSuddenSupervising = false
              app.globalData.suddensuperviseSchoolId = 0
              app.globalData.suddensuperviseSchoolName = ""
              app.globalData.suddensuperviseId = 0
            }
            dd.reLaunch({
              url: '/page/schoolIndex/schoolIndex'
            })
          },
          fail: (res) => {
            dd.alert({
              content: '网络异常',
            })
          }
        })
      }
    })
  },

  onLoad(options) {
    console.log(options);
    this.setData({
      title: options.title,
      userName: app.globalData.userName,
    });
  },
  toWorkRecord: util.throttle(function () {
    dd.httpRequest({
      url: app.baseUrl + '/DingDing/Statistics/isDutyResponsibilityPerson',
      method: 'GET',
      data: {
        departmentId: app.globalData.departmentId,
        realname: app.globalData.realname
      },
      dataType: 'json',
      success: (res) => {
        console.log("data", res.data)
        if (res.data.dutyFlag == 1) {
          dd.navigateTo({
            url: "/subpages/pages/allworkHistory/allworkHistory"
          })
        } else if (res.data.dutyFlag == 0) {
          dd.navigateTo({
            url: "/page/individualworkhistory/individualworkhistory"
          })
        }
      },
    })
  }),
});